<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 1.race   2. all   3. allSettled;

    let p1 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(111)
      }, 1000);
    })
    let p2 = new Promise((resolve, reject) => {
      setTimeout(() => {
        // resolve(222)
        reject("error");
      }, 3000);
    })
    let p3 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(333)
      }, 2000);
    })
    // Promise.allSettled([p1,p2,p3]).then(res=>{
    //     console.log(res);
    // })

    /* 
        [{status:"fulfilled",value:111},{reson:111,status:"rejected"}]
    */


    function MyallSettled (arr) {
      return new Promise(reslove => {
        // 创一个参数长度的空数组
        let resArr = new Array(arr.length);
        let count = 0;
        // console.log(resArr);
        arr.forEach((p, key) => {
          p.then(res => {
            let obj = {};
            obj.status = "fulfilled";
            obj.value = res;
            resArr[key] = obj;
            // resArr.push(obj);
            // console.log(resArr);
            count++;
            if (count === arr.length) {
              reslove(resArr)
            }
          }, err => {
            let obj = {
              status: "rejected",
              reson: err
            };
            resArr[key] = obj;
            count++;
            if (count === arr.length) {
              reslove(resArr)
            }
          })
        })
      })
    }

    // 和allSettled 方法实现的效果一样
    MyallSettled([p1, p2, p3]).then(res => {
      console.log(res);
    })


  </script>
</body>

</html>